/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.entity.component.render;

import org.chaoticengine.cgll.entity.component.RenderComponent;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.state.StateBasedGame;
import org.simpleframework.xml.Element;

/**
 * A simple rendering component for displaying the bounding rectangle of the
 * owner entity. Will be useful for debugging clipping and hit detection
 * problems.
 *
 * @author Matt v.d. Westhuizen
 */
public class FacingRenderComponent extends RenderComponent {
    private Color col = Color.red;
    private Color gradientColor = null;
    private float lineWidth = 1.0f;
    private float length = 32.0f;

    public FacingRenderComponent() {
        super();
    }

    @Element(name="color", required=false)
    public void setColor(Color c) {
        this.col = c;
    }

    @Element(name="color", required=false)
    public Color getColor() {
        return(this.col);
    }

    @Element(name="width", required=false)
    public float getLineWidth() {
        return lineWidth;
    }

    @Element(name="width", required=false)
    public void setLineWidth(float lineWidth) {
        this.lineWidth = lineWidth;
    }

    @Element(name="length", required=false)
    public float getLength() {
        return(this.length);
    }

    @Element(name="length", required=false)
    public void setLength(float length) {
        this.length= length;
    }

    @Element(name="gradient_color", required=false)
    public Color getGradientColor() {
        return(this.gradientColor);
    }

    @Element(name="gradient_color", required=false)
    public void setGradientColor(Color col) {
        this.gradientColor = col;
    }

    @Override
    public void onSetOwner() {
        // Nothing needs to be done here
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) {
        // Translation & rotation
        gr.translate(owner.getPosition().x, owner.getPosition().y);
        gr.rotate(0.0f, 0.0f, owner.getRotation());

        // Get color
        Color color = gr.getColor();

        // Get line width
        float lw = gr.getLineWidth();

        // Rendering
        gr.setLineWidth(lineWidth);
        gr.setColor(this.col);

        if (gradientColor == null) {
            gr.drawLine(0.0f, 0.0f, 0.0f, -length);
        } else {
            gr.drawGradientLine(0.0f, 0.0f, color, 0.0f, -length, gradientColor);
        }

        // Restore line width
        gr.setLineWidth(lw);

        // Restore color
        gr.setColor(color);

        // Undo translation & rotation
        gr.rotate(0.0f, 0.0f, -owner.getRotation());
        gr.translate(-owner.getPosition().x, -owner.getPosition().y);

    }
}
